package com.kgc.controller.admin;

import com.kgc.common.Result;
import com.kgc.entity.User;
import com.kgc.service.interf.admin.AdminService;
import com.kgc.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hyj
 * @time 2024/4/3 11:27
 * @describe
 */
@RestController
@RequestMapping("/apis/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public String login(@RequestBody User user) throws Exception {
        User login = adminService.adminLogin(user.getUsername(), user.getPassword());
        if (login != null && login.getUserId().equals(-1)) {
            return Result.success("2", "用户名或邮箱暂未注册，登录失败");
        } else if (login != null && login.getUserId().equals(-2)) {
            return Result.success("1", "密码错误，登录失败");
        } else if (login != null && login.getUserId().equals(-3)) {
            return Result.success("4", "该用户已被禁用，登录失败");
        } else if (login != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", login.getUserId());
            map.put("username", login.getUsername());
            login.setToken(JwtUtil.generateJwt(map));
            return Result.success("0", "登录成功", login);
        }
        return Result.success("3", "权限不足，登录失败");
    }
}
